package com.diamond.service.impl;

import java.util.List;
import java.util.Map;

import com.diamond.bo.ClientOrderDetailBO;
import com.diamond.bo.QueryOrderBO;
import com.diamond.bo.ReturnBO;
import com.diamond.po.ClientOrderDetailPO;
import com.diamond.service.IClientorderdetailService;
import com.diamond.util.StringUtil;

/**
 * @author JSmart Tools
 */
@SuppressWarnings("unchecked")
public class ClientorderdetailService extends Service<ClientOrderDetailPO> implements IClientorderdetailService {

	public ReturnBO<ClientOrderDetailBO> queryClientOrderDetail(QueryOrderBO queryOrderBo) throws Exception {
		ReturnBO<ClientOrderDetailBO> returnBo = new ReturnBO<ClientOrderDetailBO>();
		StringBuilder queryDataSql = new StringBuilder("select distinct * from clientorderdetail cd, clientorder c, user u where cd.cliOrdMaster_id=c.cliOrdMaster_id and c.user_id=u.user_id and c.del_state = 0");
		StringBuilder querySizeSql = new StringBuilder("select distinct * from clientorderdetail cd, clientorder c, user u where cd.cliOrdMaster_id=c.cliOrdMaster_id and c.user_id=u.user_id and c.del_state = 0");
		if (null != queryOrderBo.getBatch() && !"".equals(queryOrderBo.getBatch())) {
			queryDataSql.append(" and cd.batch like '%").append(queryOrderBo.getBatch()).append("%'");
			querySizeSql.append(" and cd.batch like '%").append(queryOrderBo.getBatch()).append("%'");
		}
		if (null != queryOrderBo.getOrderDateFrom() && !"".equals(queryOrderBo.getOrderDateFrom())
				&& null != queryOrderBo.getOrderDateTo() && !"".equals(queryOrderBo.getOrderDateTo())) {
			queryDataSql.append(" and cd.order_date between '").append(queryOrderBo.getOrderDateFrom()).append("' and '").append(queryOrderBo.getOrderDateTo()).append("'");
			querySizeSql.append(" and cd.order_date between '").append(queryOrderBo.getOrderDateFrom()).append("' and '").append(queryOrderBo.getOrderDateTo()).append("'");
		}
		if (null != queryOrderBo.getClientName() && !"".equals(queryOrderBo.getClientName())) {
			queryDataSql.append(" and u.user_name='").append(queryOrderBo.getClientName()).append("'");
			querySizeSql.append(" and u.user_name='").append(queryOrderBo.getClientName()).append("'");
		}
		if (null != queryOrderBo.getState() && !"".equals(queryOrderBo.getState())) {
			queryDataSql.append(" and cd.state='").append(queryOrderBo.getState()).append("'");
			querySizeSql.append(" and cd.state='").append(queryOrderBo.getState()).append("'");
		}
		if (null != queryOrderBo.getUserId() && !"".equals(queryOrderBo.getUserId())) {
			queryDataSql.append(" and u.user_id=").append(queryOrderBo.getUserId());
			querySizeSql.append(" and u.user_id=").append(queryOrderBo.getUserId());
		}
		queryDataSql.append(" order by cd.order_date desc").append(" limit ").append(queryOrderBo.getStart()).append(", ").append(queryOrderBo.getPageSize());
		List<ClientOrderDetailBO> datalist = super.genericDAO.findBySqlToObj(queryDataSql.toString(), ClientOrderDetailBO.class);
		List dataSizelist = super.genericDAO.findBySqlToObj(querySizeSql.toString(), null);
		returnBo.setTotalSize(dataSizelist.size());
		returnBo.setReturnBolist(datalist);
		returnBo.setPageSize(queryOrderBo.getPageSize());
		returnBo.setStart(queryOrderBo.getStart());
		return returnBo;
	}

	public Boolean updateClientOrderDetailState(String cliOrdMasterId, List list) throws Exception {
		Boolean result = false;
		Integer updateCliFlag = 0;
		Integer updateSupFlag = 0;
		for (int u = 0; u < list.size(); u++) {
			Map parm = (Map) list.get(u);
			String cliOrdrDetailId = String.valueOf(parm.get("cliOrdrDetailId"));
			String supplierName = String.valueOf(parm.get("supplierName"));
			String state = String.valueOf(parm.get("state"));
			String payState = String.valueOf(parm.get("payState"));
			String deliverGoodsState = String.valueOf(parm.get("deliverGoodsState"));
			
			if (null != cliOrdrDetailId && !"".equals(cliOrdrDetailId) && null != state && !"".equals(state)
					&& null != payState && !"".equals(payState) && !StringUtil.isEmpty(deliverGoodsState)) {
				String updateCliSql = "update clientorderdetail cd set cd.state='" + state + "', cd.pay_state='" + payState + "', cd.deliver_goods_state='"+deliverGoodsState+"' where cd.cliOrdrDetail_id=" + cliOrdrDetailId;
				String updateSupSql = "UPDATE supporderdetail sd set sd.state='" + state + "' where sd.supplier_name='" + supplierName + "' and sd.batch in (select cd.batch from clientorderdetail cd where cd.cliOrdrDetail_id="+cliOrdrDetailId+")";
				updateCliFlag = super.genericDAO.updateBySql(updateCliSql);
				updateSupFlag = super.genericDAO.updateBySql(updateSupSql);
			}
		}
		if (updateCliFlag > 0) {
			String queryCliMasterSql = "select cd.* from clientorderdetail cd, clientorder c where cd.cliOrdMaster_id = c.cliOrdMaster_id and c.cliOrdMaster_id=" + cliOrdMasterId;
			List<ClientOrderDetailBO> clientOrderDetailBolist = super.genericDAO.findBySqlToObj(queryCliMasterSql, ClientOrderDetailBO.class);
			Integer detailSize = clientOrderDetailBolist.size();
			int stateFlag = 0;
			Double midTotal = 0d;
			Double total = 0d;
			for (ClientOrderDetailBO clientOrderDetailBo : clientOrderDetailBolist) {
				if ("0".equals(clientOrderDetailBo.getState())) {
					stateFlag++;
				}
				if (!"2".equals(clientOrderDetailBo.getState())) {
					midTotal = midTotal + clientOrderDetailBo.getMidPrice();
					total = total + clientOrderDetailBo.getPrice();
				}
			}
			String masterState = "";
			if (stateFlag == detailSize) {
				masterState = "0";
			} else if (stateFlag < detailSize && stateFlag > 0) {
				masterState = "1";
			} else if (stateFlag == 0) {
				masterState = "2";
			}
			String updateCliMasterSql = "update clientorder c set c.order_status='" + masterState + "', c.mid_total="+midTotal+", c.total="+total+" where c.cliOrdMaster_id=" + cliOrdMasterId;
			Integer updateCliMFlag = super.genericDAO.updateBySql(updateCliMasterSql);
			if (updateCliMFlag > 0) {
				result = true;
			}
		}
		return result;
	}
}
